package org.sidora.strata.explorer;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.logging.Level;
import javax.swing.DefaultListModel;
import javax.swing.UIManager;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.sidora.core.Site;
import org.sidora.core.Zone;
import org.sidora.core.util.LoggerProvider;
import org.sidora.core.util.ParameterCollector;
import org.sidora.strata.SidoraTools;
import org.sidora.strata.Storage;


/**
 * Zone Explorer Form
 * @author Enric Tartera, Juan Manuel Gimeno, Roger Masgoret
 * @version 1.0
 */
final class ZoneExplorerTopComponent extends TopComponent {
    
    private static ZoneExplorerTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    
    private static final String PREFERRED_ID = "ZoneExplorerTopComponent";
    
    private ZoneExplorerTopComponent() {
        System.setProperty(
            "Quaqua.tabLayoutPolicy","wrap"
         );
         // set the Quaqua Look and Feel in the UIManager
         try { 
              UIManager.setLookAndFeel(
                  "ch.randelshofer.quaqua.QuaquaLookAndFeel"
              );
         // set UI manager properties here that affect Quaqua
         
         } catch (Exception e) {
             // take an appropriate action here
             
         }
         // insert your application initialization code here
        initComponents();
        setName(NbBundle.getMessage(ZoneExplorerTopComponent.class, "CTL_ZoneExplorerTopComponent"));
        setToolTipText(NbBundle.getMessage(ZoneExplorerTopComponent.class, "HINT_ZoneExplorerTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jListZone = new javax.swing.JList();
        jListZone.putClientProperty(
            "Quaqua.List.style", "striped"
        );
        jButton1 = new javax.swing.JButton();
        jButton1.putClientProperty(
            "Quaqua.Button.style","square"
        );
        jButton2 = new javax.swing.JButton();
        jButton2.putClientProperty(
            "Quaqua.Button.style","square"
        );

        jListZone.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListZoneValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListZone);

        org.openide.awt.Mnemonics.setLocalizedText(jButton1, "-");

        org.openide.awt.Mnemonics.setLocalizedText(jButton2, "+");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jButton2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton1))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                .add(0, 0, 0))
        );

        layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2)))
        );

        layout.linkSize(new java.awt.Component[] {jButton1, jButton2}, org.jdesktop.layout.GroupLayout.VERTICAL);

    }// </editor-fold>//GEN-END:initComponents

    private void jListZoneValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListZoneValueChanged
       System.out.println("123");
       /* if (jListZone.getValueIsAdjusting()){
            SidoraTools.showElement();
            Storage.setZone((Zone) jListZone.getSelectedValue());
            Storage.getSectorExplorerAction().fillSectorItems();
        }
        */
       SidoraTools.showElement();
       Storage.setZone((Zone) jListZone.getSelectedValue());
       Storage.getSectorExplorerAction().fillSectorItems();
    }//GEN-LAST:event_jListZoneValueChanged
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        addZone();
        fillZoneItems();
    }//GEN-LAST:event_jButton2ActionPerformed
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JList jListZone;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
    
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized ZoneExplorerTopComponent getDefault() {
        if (instance == null) {
            instance = new ZoneExplorerTopComponent();
        }
        return instance;
    }
    
    /**
     * Obtain the ZoneExplorerTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized ZoneExplorerTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            ErrorManager.getDefault().log(ErrorManager.WARNING, "Cannot find ZoneExplorer component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof ZoneExplorerTopComponent) {
            return (ZoneExplorerTopComponent)win;
        }
        ErrorManager.getDefault().log(ErrorManager.WARNING, "There seem to be multiple components with the '" + PREFERRED_ID + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }
    
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }
    
    public void componentOpened() {
        this.setDisplayName("Zone");
    }
    
    public void componentClosed() {
        // TODO add custom code on component closing
    }
    
    /** replaces this in object stream */
    public Object writeReplace() {
        return new ResolvableHelper();
    }
    
    protected String preferredID() {
        return PREFERRED_ID;
    }
    
    final static class ResolvableHelper implements Serializable {
        private static final long serialVersionUID = 1L;
        public Object readResolve() {
            return ZoneExplorerTopComponent.getDefault();
        }
    }
    
    private static void addZone(){
        Site site = Storage.getSite();
        if (site != null){
            ParameterCollector param = new ParameterCollector();
            Zone.Inventory zi = new Zone.Inventory();
            
            param.put("site",site);
            
            Zone zone = null;
            try {
                zone = zi.getNewObject(param);
                zone.setDescription("New Zone");
                
                zi.setObject(zone);
            }catch (Exception e1) {
                LoggerProvider.getInstance().log(Level.WARNING,"Exception",e1);
            }
        }else{
            LoggerProvider.getInstance().fine("No hay un Site seleccionado.");
        }
    }
    
    public void fillZoneItems(){
        Site site = Storage.getSite();
        if (site != null){
            DefaultListModel zoneModel = new DefaultListModel();
            
            try {
                List list = Zone.Inventory.getList(site);//Site.Inventory.getList();
                TreeSet <Zone> zones = new TreeSet<Zone>();
                
                Iterator iter2 = list.iterator();
                while(iter2.hasNext()){
                    Zone zone = (Zone) iter2.next();
                    zones.add(zone);
                }
                
                //Ho fiquem en un TreeSet per a tenir la col�lecci� ordenada
                Iterator iter = zones.iterator();
                while (iter.hasNext()){
                    zoneModel.addElement(iter.next());
                }
                
            } catch (Exception e1) {
                LoggerProvider.getInstance().log(Level.WARNING,"Exception",e1);
            }
            jListZone.setModel(zoneModel);
        } else {
            jListZone.setModel(new DefaultListModel());
        }
    }
}
